import legend from '@/common/legend.js'
export default {
	// 根据key 获取图例
	getLegendItem(type) {
		const legendList = uni.getStorageSync('legend')
		let item = legendList.find((item) => item.type == type)
		if (!item) {
			let locallegend = legend[type]
			if (!locallegend) {
				uni.showToast({
					icon: 'error',
					title: '获取图例失败'
				})
			}

			return locallegend || {}
		}
		return item
	},
	// 根据key生成图例对象，可以传入点击事件
	getLegendOptionsByKey(type, callback) {
		let item = this.getLegendItem(type)

		let legendOptions = {
			id: 'legend-item-' + type,
			legendData: item,
			layout: 'h',
			filterDir: item.order,
			labelAlign: 'inside',
			labelPosition: 'right',
			labelClass: 'legend-label',
			titleClass: 'legend-title',
			unitClass: 'legend-unit',
			itemHeight: '28',
			textGapShow: type == 'uv10m' || type == 'V' || type == 'F' ? 2 : 1,
			onItemClick: (value) => {
				callback(value)
			}
		}
		return legendOptions
	},
	//根据值获取图例颜色
	getColorIndex(legend, value) {
		let bgColor = ''
		let index = ''
		// 图例为倒序
		if (legend.order === 'desc') {
			for (var i = 0; i < legend.items.length; i++) {
				if (value >= legend.items[i].min) {
					bgColor = legend.items[i].color
					index = i
					break
				}
			}
		} else {
			for (var j = legend.items.length - 1; j >= 0; j--) {
				if (value >= legend.items[j].min) {
					bgColor = legend.items[j].color
					index = j
					break
				}
			}
		}
		return { bgColor, index }
	},
	// 图片预加载
	preLoadImg(url) {
		var img = new Image()
		img.src = url
	}
}
